Working with lizards

Get the lizards data

lizards <- read_csv(here::here("data_tidy", "lizards.csv"))
## Rows: 1628 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): date, scientific_name, common_name, zone, site, plot, spp, sex, rc...
## dbl  (6): pit, toe_num, sv_length, total_length, weight, pc
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Make a really bad histogram of lizard weights

ggplot(lizards, aes(x = weight)) +
  geom_histogram(fill = "orange", 
                 color = "black", 
                 size = 0.2, #width of the border 
                 linetype = "dotted")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

## Create a less ugly graph

ggplot(data = lizards, aes(x = total_length, y = weight)) + 
  geom_point(shape = 22, 
             size = 3,
             alpha = 0.5,
             aes(color = common_name))

Make a very ridiculous, bad idea graph

Bubble plot where the color of the points changes based on the common_name and the size of points varies based on the total_length

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name, 
                 size = total_length), 
             shape = 22, 
             fill = "black") + 
  theme_minimal()

Create something not quite as terrible

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name), 
             size = 0.8) +
  theme_light() + 
  facet_wrap(~common_name, ncol = 4)

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name), 
             size = 0.8) + 
  facet_grid(sex ~ tail)

I don’t even know what at this point

lizard_counts <- lizards %>% 
  group_by(common_name) %>% 
  summarize(count = n())

# Same thing: but it is better to use summarize if you also need to do mean, std and other stuff 
#lizard_counts <- lizards %>% dplyr::count(common_name)

lizard_counts_cn_tail <- lizards %>% count(common_name, tail)
ggplot(data = lizard_counts, aes(y = fct_reorder(common_name, -count), x = count)) +
  geom_col(aes(fill = common_name), show.legend = FALSE) +
  labs(title = "Total Number of Lizard by Species", 
       y = "Common Name", 
       x = "Total Number of Lizards")

Try converting common name to an ordered factor outside of ggplot

lizard_counts <- lizard_counts %>% 
  mutate(common_name = fct_reorder(common_name, count))

Axis scale breaks, limits & labels

Scatter plot

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name)) +
  scale_x_continuous(breaks = c(0, 5, 50, 500), 
                     limits = c(0, 500), 
                     expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

lizard_counts <- lizards %>% 
  mutate(date = lubridate::mdy(date)) %>% 
  group_by(date) %>% 
  summarize(count = n())
ggplot(data = lizard_counts, aes(x = date, y = count)) +
  geom_line(aes(color = count), show.legend = FALSE) +
  scale_x_date(date_breaks = "3 years", 
               date_labels = "%y")

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = weight)) +
  scale_color_stepsn(colors = c("green", "blue", "purple"), 
                     breaks = c(0, 20, 40))

Update a color scheme using a palette in paletteer

Make a horizontal boxplot with common_name on the y_axis, total_length on the x, with color changing based on common_name

lizards_fct <- lizards %>% 
  mutate(common_name = fct_reorder(common_name, total_length, .fun = median))


ggplot(data = lizards_fct, aes(y = common_name, x = total_length)) +
  geom_boxplot(aes(fill = common_name), show.legend = FALSE) +
  scale_fill_paletteer_d(palette = "khroma::bright")

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point() +
  theme(
    panel.grid = element_blank(), 
    plot.background = element_rect(fill = "cyan4"), 
    axis.text.x = element_text(color = "orange"), 
    axis.title = element_text(color = "yellow", size = 10)
  )

# Day 9 Work: Part 2 of data visualization

ggrepel

ww_lizards <- lizards %>% 
  filter(common_name == "western whiptail", site == "sand")

ggplot(data = ww_lizards, aes(x = total_length, y = weight)) +
  geom_point() +
  geom_text_repel(aes(label = toe_num), size = 2, max.overlaps = 20)

Using gapminder dataset

gapminder_Europe <- gapminder %>% 
  filter(continent == "Europe",  year == "2007")

ggplot(data = gapminder_Europe, aes(x= gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_text_repel(aes(label = country), size = 3)

gghighlight

p <- ggplot(lizards, aes(x = total_length, y = weight)) +
  geom_point()

p + gghighlight(toe_num == 250, label_key = toe_num)

q <- ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_line(aes(color = common_name)) +
  gghighlight(max(weight) > 30)
## label_key: common_name
q 

Patchwork for compoint figures

p / q & # p is on top of q 
  theme_minimal()

p | q #these are side by side 

## A few new graph types

Marginal plots

whiptails <- lizards %>% 
  filter(common_name == "western whiptail") %>% 
  drop_na(total_length, weight)

ggplot(data = whiptails, aes(x = total_length, y = weight)) +
  geom_point() +
  geom_rug()

Marginal plot with boxplots

my_plot <- ggplot(whiptails, aes(x = total_length, y = weight)) +
  geom_point(aes(color = sex), size = 2, shape = 22) +
  scale_color_manual(
    values = c("cyan3", "black", "gold"),
    name = "Sex:",
    labels = c("Female", "Juvenile", "Male")
  ) +
  theme_minimal() +
  theme(legend.position = "bottom")

ggMarginal(my_plot, type = "boxplot", groupColour = TRUE)

Beeswarm plot

ggplot(data = whiptails, aes(x = sex, y = weight)) +
  geom_beeswarm(aes(color = sex)) +
  geom_boxplot(fill = NA)

Heatmap with geom_tile()

lizards_counts <- lizards %>% 
  mutate(date = lubridate::mdy(date)) %>% 
  group_by(year = lubridate::year(date), common_name) %>% 
  summarize(count = n())
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
ggplot(data = lizards_counts, aes(x = year, y = common_name)) +
  geom_tile(aes(fill = count)) +
  geom_text(aes(label = count), color = "white", size = 2) +
  scale_fill_viridis_c()

Make a map

jornada_vegetation <- read_sf(here("data_raw", "spatial_vegetation", "doc.kml")) %>% 
  select(Name) %>% 
  clean_names()

ggplot(data = jornada_vegetation) +
  geom_sf(aes(fill = name), color = "white", size = 0.2) +
  scale_fill_paletteer_d("ggthemes::manyeys") +
  labs(x = "Longitude", 
       y = "Latitude", 
       fill = "Dominant Vegetation") +
  theme_minimal()